<?php
include_once('includes.php'); 
include_once('includes/rating.class.php');	

if(isset($_GET["id"]) && $_GET["id"]>0 )
{
	$id = (int) $_GET["id"];
	$sql = "select  matchs.*, competition.name as cname, competition.id as cid,competition.classicon as cicon  from matchs left join competition on matchs.competition_id = competition.id  where matchs.id=".addslashes($id);
	$match=db_fetch_one($sql);
	if(!$match)
	{
		Header("Location: /");
		exit;
	}
	//link channel----------------------------
	$sql = "select * from channel where match_id=".addslashes($id)." order by vote desc";
	$channels=db_fetch_all($sql);
	//----------------------------
	$t1 = db_fetch_one("SELECT * FROM team WHERE id = $match->team1_id");
	$team1 = $t1->name;
	$team1_info = db_fetch_one("SELECT * 
								FROM team_info 
								WHERE name in (	SELECT namphan 
												FROM team_name_matching 
												WHERE luanvo = '$team1')");
	$t2 = db_fetch_one("SELECT * FROM team WHERE id = $match->team2_id");
	$team2 = $t2->name;
	$team2_info = db_fetch_one("SELECT * 
								FROM team_info 
								WHERE name in (	SELECT namphan 
												FROM team_name_matching 
												WHERE luanvo = '$team2')");
}	
else
{
	Header("Location: /");
	exit;
}

$local_kick_off = convertTimeToLocal($match->kick_off,$match->timezone);
//$local_kick_off = "2011-09-21 17:05:00";
$now = convertTimeToLocal( date("Y-m-d H:i:s") , date("O")/100 );		
$difference = strtotime($local_kick_off) - strtotime($now); 
$minremain =  round($difference/60);

//-------------------------------------------
$page_title = $match->versus." (".formatOutputDate($local_kick_off).")";
?>

<?php include_once('includes/header.php'); 	 ?>

<script type="text/javascript" src="/js/jquery.fancybox/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript" src="/js/jquery.fancybox/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="/js/jquery.fancybox/fancybox/jquery.fancybox-1.3.4.css" media="screen" />


<script type="text/javascript" src='/js/jquery-ui/js/jquery-ui-1.8.11.custom.min.js'></script>
<script type="text/javascript" src='/js/jquery-ui/js/jquery.ui.stars/jquery.ui.stars.js'></script>
<link  rel="stylesheet" type="text/css"  href='/js/jquery-ui/js/jquery.ui.stars/crystal-stars.css'/>
    
	
<script type="text/javascript">
$(document).ready(function() {
	
	$("#addchannel").fancybox({"padding":0 , "hideOnOverlayClick":false ,"showCloseButton":false});	
	
	<?php
	echo '$("#popup_help").fancybox({
				"padding":0 , 
				"hideOnOverlayClick":false ,
				"showCloseButton":false,
				"onClosed": function() { 
					$("#player").fadeIn();
					$("#MediaPlayer").focus();			
				} ,
				"onStart": function() { 
					$("#player").fadeOut();	
				} 
				
			}) ;' ;
			
	/*	$isDownloadedP2PFilter = $_COOKIE["isDownloadedP2PFilter"] ;
		if($isDownloadedP2PFilter)
			echo '$("#popup_help").fancybox({
				"padding":0 , 
				"hideOnOverlayClick":false ,
				"showCloseButton":false,
				"onClosed": function() { 
					$("#player").fadeIn();
					$("#MediaPlayer").focus();			
				} ,
				"onStart": function() { 
					$("#player").fadeOut();	
				} 
				
			}) ;' ;
		else 
			echo '$("#popup_help").fancybox({
				"padding":0 , 
				"hideOnOverlayClick":false ,
				"showCloseButton":false,
				"onClosed": function() { 
					$("#player").fadeIn();	
					$("#MediaPlayer").focus();
				} ,
				"onStart": function() { 	
					$("#player").fadeOut();	
				} 
				
			}).trigger("click");' ;
		*/	
	?>	
	
	play('<?php echo $channels[0]->id;?>','<?php echo $channels[0]->link;?>');

	initChatBox();
});

</script>	

<div class="help-link"> 
	<a id='popup_help' href='/popup-help'>Hướng dẫn xem trực tuyến</a> 
</div>
<span class="match-competition <?php echo $match->cicon; ?>"><a href="/competition/<?php echo $match->cid."-".clean_entities($match->cname);?>" title="<?php echo $match->cname;?>"><?php echo $match->cname;?></a></span> 
<h1 class="header">
<?php
if($team1_info != null)
{
?>
<a id="player_<?php echo str_replace(" ","_",trim($team1)); ?>" href="/team-info/<?php echo $match->team1_id . "-" . str_replace(" ","-",trim($team1)); ?>" onmouseover="show_tip(&quot;player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>&quot;);" onmouseout="hide_tip(&quot;player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>&quot;);">
	<?php echo $team1; ?>
</a>
<div class="xtip" id="player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>" style="visibility: hidden;">
	<div class="ballon">
		<div class="tla" id="tla_player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>" style="display: inline;">
		</div>
		<div class="tra" id="tra_player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>" style="display: none;">
		</div>
		<div id="balloonBody">
			<h4>
			</h4>
			<div class="boxBody">
				<?php echo $team1_info->html; ?>
			</div>
			<div class="boxFooter">
				<p>
				</p>
			</div>
		</div>
		<div class="bla" id="bla_player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>" style="display: none;">
		</div>
		<div class="bra" id="bra_player_tip_<?php echo str_replace(" ","_",trim($team1)); ?>" style="display: none;">
		</div>
	</div>
</div>
<?php
}
else
{
	echo $team1;
}
?>
-
<?php
if($team2_info != null)
{
?>
<a id="player_<?php echo str_replace(" ","_",trim($team2)); ?>" href="/team-info/<?php echo $match->team2_id . "-" . str_replace(" ","-",trim($team2)); ?>" onmouseover="show_tip(&quot;player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>&quot;);" onmouseout="hide_tip(&quot;player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>&quot;);">
	<?php echo $team2; ?>
</a>
<div class="xtip" id="player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>" style="visibility: hidden;">
	<div class="ballon">
		<div class="tla" id="tla_player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>" style="display: inline;">
		</div>
		<div class="tra" id="tra_player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>" style="display: none;">
		</div>
		<div id="balloonBody">
			<h4>
			</h4>
			<div class="boxBody">
				<?php echo $team2_info->html; ?>
			</div>
			<div class="boxFooter">
				<p>
				</p>
			</div>
		</div>
		<div class="bla" id="bla_player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>" style="display: none;">
		</div>
		<div class="bra" id="bra_player_tip_<?php echo str_replace(" ","_",trim($team2)); ?>" style="display: none;">
		</div>
 	</div>
</div>
<?php
}
else
{
	echo $team2;
}
?>
</h1>

<span class="match-kickoff"><?php echo formatOutputDate($local_kick_off); ?></span> <br/>

<div style="padding:10px 0">
	<!-- AddThis Button BEGIN -->
	<div class="addthis_toolbox addthis_default_style ">
		<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
		<a class="addthis_button_tweet"></a>
		<a class="addthis_button_google_plusone" g:plusone:size="medium"></a>	
	</div>
	<script type="text/javascript" src="/js/addthis_widget.js"></script>
	<!-- AddThis Button END -->
</div>

<div class="clearfix">
<?php
if( $match->status == 2 )//match end
{
	echo "<div class='mgsbox'>Trận đấu đã kết thúc.</div>";	
}
//echo "<a id='addchannel' href='/popup-addchannel?id=".$id."'>Click here</a> ";
	
else
{ 
	if($minremain>0)
	echo '<div class="mgsbox">'.remainTime($local_kick_off,'<div class="remainingTime countdown"><span class="NoDisplay">Trận đấu sẽ diễn ra sau <b class="remainingDay">%d </b> ngày <b class="remainingHour">%h </b> giờ <b class="remainingMinute">%m </b> phút <b class="remainingSecond">0</b> giây</span><span class="TimeDisplay"><span class="Text"></span><span class="Time"></span></span></div>').'</div>';
		
}
?>

<?php
if(count($channels)>0 )
{
?>

<div id="player-cover">
	<div id="player"> 
	</div>
</div>

<div class="channel">
	<ul class="list-channel">
		<?php
		foreach($channels as $channel){
		?>
			<li id="chanel_<?php echo $channel->id;?>" >
				<a  href="javascript:;" onclick="play('<?php echo $channel->id;?>','<?php echo $channel->link;?>')" title="<?php echo $channel->name;?>"><?php echo $channel->name;?></a> 
				
				
				<div class="rateContainer" >
					<div id="stars-wrapper_<?php echo $channel->id;?>">
						<?php 
						$rating = new Rating();
						$rating->target_id = $channel->id;
						echo $rating->showRating();
						?>
						<script>initRating(<?php echo $channel->id;?>);</script>
					</div> 
					<div class="rateLoader"><div style="padding-top: 5px;">please wait...</div></div>
				</div>	
				
				
			</li>
		<?php
		}
		?>
	</ul>
	
</div>
<?php
}
?>

<?php 
if($match->more_info<>"")
	echo "<div>".$match->more_info."</div>";
?>
	
</div>


<div class="social-share">
	<span class="text">Chia sẻ</span>
	<!-- AddThis Button BEGIN -->
	<div class="content">
		<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
			<a class="addthis_button_facebook"></a>
			<a class="addthis_button_google"></a>
			<a class="addthis_button_twitter"></a>
			<a class="addthis_button_compact"></a>
			<a class="addthis_counter addthis_bubble_style"></a>
		</div>
	</div>
	<!-- AddThis Button END -->
</div>



<?php
//start box matchs Now playing
$sql = "select matchs.*, competition.name as cname,competition.classicon as cicon  from matchs left join competition on matchs.competition_id = competition.id  where  status=1  and matchs.id<>".$match->id." order by kick_off asc limit 5 ";
$matchs=db_fetch_all($sql);
if(count($matchs)>0) 
{
?>	
<div class="cbox clearfix">
	<span class="title">Các trận đang diễn ra</span>
	<div class="content">
		<ul class="matchs">
	<?php 
	
	foreach($matchs as $match)
	{
		$local_kick_off = convertTimeToLocal($match->kick_off,$match->timezone);
	?>
		<li class="<?php echo $match->cicon; ?>"> 
			<div class="cname"><?php echo $match->cname; ?></div>
			<div class="kick-off"><?php echo formatOutputDate($local_kick_off); ?></div>
			<div class="vs">	<a href="/match/<?php echo $match->id."-".clean_entities($match->versus); ?>"><strong><?php echo $match->versus; ?></strong></a>  </div>
			
		</li>
	<?php
	}
	?>	
		<span class="morelink"> <em><a href="/live-streaming" title="Xem tất cả lịch trực tiếp các trận sắp diễn ra">Xem tất cả...</a> </em> </span>
		</ul>
	</div>
</div>	
<?php
}
?>

<?php include_once('includes/footer.php'); ?>